package com.xiaoyu;

import com.xiaoyu.model.Score;
import com.xiaoyu.model.Student;
import com.xiaoyu.service.ScoreService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "成绩信息接口")
@RestController
//@CrossOrigin(origins = "http://39.101.65.116:8080")
@CrossOrigin(origins = "http://localhost:8080")
public class ScoreController {
    @Autowired
    private ScoreService scoreService;

    @ApiOperation("学生成绩信息查询接口")
    @GetMapping("/scoreInfo")
    public List<Score> scoreInfo(HttpSession session){
        Student s=(Student) session.getAttribute("student");
        if (s!=null){
            Score score=new Score();
            score.setSid(s.getId());
            return scoreService.allScore(score);
        }
        return null;
    }

    @ApiOperation("教师成绩查询接口")
    @GetMapping("/scoreInfoT")
    public List<Score> scoreInfoT(){
        return scoreService.allScore(null);
    }

    @ApiOperation("成绩更新接口")
    @PostMapping("/score")
    public String updateScore(@RequestBody Score score){
        Score newScore=new Score();
        newScore.setSid(score.getSid());
        newScore.setCid(score.getCid());
        newScore.setCourseId(score.getCourseId());
        Score s=scoreService.findOneScore(newScore);
        if(s!=null){
            score.setId(s.getId());
            if(scoreService.updateScore(score))
                return "update";
            else
                return "error";
        }else{
            if(scoreService.addScore(score))
                return "add";
            else
                return "error";
        }
    }

    @ApiOperation("成绩列表接口")
    @GetMapping("/scoreList")
    public List<Score> scoreList(){
        return scoreService.allScore(null);
    }
}
